#include<iostream>
using namespace std;
#include<math.h>

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        if(root == NULL) {
            return true;
        }
        bool isRB = this->isBalanced(root->right);
        int hR = this->f(root->right);
        bool isLB = this->isBalanced(root->left); 
        int hL = this->f(root->left);
        if(abs(hR-hL) < 2 && isRB && isLB) {
            return true;
        }
        return false;
    }
    //获取树的高度
    int f(TreeNode* root) {
        if(root == NULL) {
            return 0;
        }
        int Rheight = f(root->right);
        int Lheight = f(root->left);
        int maxHeight = Rheight > Lheight ? Rheight : Lheight;
        return maxHeight+1;
    }
};

int main(){
    return 0;
}